package com.wujw.netty.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Desc:
 *
 * @author wujw
 * @email jiwenwu@outlook.com
 * @date 2018/11/13
 */
public class BIOClient {
    // 1.向服务端发送连接
    // 2.向服务端传递参数
    // 3.从服务端接收参数

    private static Integer PORT = 8888;
    private static String IP_ADDRESS = "127.0.0.1";

    public static void main(String[] args) {
        for (int i = 0; i < 6; i++) {

            clientRequest("nihao"+i);
        }
    }

    private static void clientRequest(String message) {
        Socket socket = null;
        BufferedReader reader = null;
        PrintWriter writer = null;

        try {
            socket = new Socket(IP_ADDRESS,PORT);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream(),true);
            writer.println("发送expression:" + message);
            System.out.println(message + "客户端返回数据：" + reader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (null != reader) {
                    reader.close();
                }
                if (null != socket) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
